<?php

namespace App\Admin\Extensions;

use App\Admin\Models\Users;
use Encore\Admin\Facades\Admin;

class Check
{
    protected $id;
    protected $url;
    protected $go;
    protected $msg;
    protected $type;
    protected $_token;
    protected $refuse = Users::USER_STATUS_REFUSE;
    protected $success = Users::USER_STATUS_SUCCESS;

    public function __construct($type, $id, $url, $go, $msg) {
        $this->id = $id;
        $this->url = $url;
        $this->go = $go;
        $this->msg = $msg;
        $this->type = $type;
        $this->_token = csrf_token();
    }

    protected function agree(){
        return <<<SCRIPT
        $('.grant-agree{$this->id}').off('click').on('click', function() {
            var id = $this->id;
            var url = '$this->url';
            var go = '$this->go';
            var msg = '$this->msg';
            bootbox.confirm({
                message: "确定要"+msg+"吗?",
                buttons: {
                    confirm: {
                        label: '确定',
                        className: 'btn-success'
                    },
                    cancel: {
                        label: '取消'
                    }
                },
                callback: function(result) {
                    if (!result) {
                        return;
                    }
                    $.post(
                        url, {
                            id: id,
                            stat: $this->success,
                            _token: '$this->_token'
                        },
                        function(ret) {
                            if (ret.code == 'success') {
                                swal(ret.msg, '', 'success');
                                setTimeout(function(){
                                    window.location.reload();
                                },1000)
                            } else {
                                swal(ret.msg, '', 'error');
                            }
                        }
                    )
                }
            });
        });
SCRIPT;
    }

    protected function refuse() {
        return <<<SCRIPT
        $('.grant-refuse{$this->id}').off('click').on('click', function() {
            var id = $this->id;
            var url = '$this->url';
            var go = '$this->go';
            var msg = '$this->msg';
            bootbox.prompt({
                title: "确定要"+msg+"吗? 请填写原因", 
                buttons: {
                    confirm: {
                        label: '确定',
                        className: 'btn-success'
                    },
                    cancel: {
                        label: '取消'
                    }
                },
                centerVertical: true,
                callback: function(result){ 
                    if (!result) {
                        return;
                    }
                    $.post(
                        url, {
                            id:id,
                            stat: $this->refuse,
                            reason: result,
                            _token: '$this->_token'
                        },
                        function(ret) {
                            if (ret.code == 'success') {
                                swal(ret.msg, '', 'success');
                                setTimeout(function(){
                                    window.location.reload();
                                },1000)
                            } else {
                                swal(ret.msg, '', 'error');
                            }
                        }
                    ) 
                }
            });
        });
SCRIPT;
    }

    protected function render() {
        Admin::js('/js/bootbox.min.js');
        if($this->type){
            Admin::script($this->agree());
            return "<a class='btn btn-xs btn-success grant-agree{$this->id}' data-id='{$this->id}'>{$this->msg}</a>&nbsp;";
        }else{
            Admin::script($this->refuse());
            return "<a class='btn btn-xs btn-danger grant-refuse{$this->id}' data-id='{$this->id}'>{$this->msg}</a>&nbsp;";
        }
    }

    public function __toString() {
        return $this->render();
    }
}